package com.example.studyFile.asynchronousOptimization;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;

public class ValidationExecutor2 {

    public static void main(String[] args) {
        int cpuCores = Runtime.getRuntime().availableProcessors(); // 获取CPU核心数
        System.out.println("cpuCores = " + cpuCores);
        // 创建自定义线程池
        ExecutorService executorService = new ThreadPoolExecutor(
                4, // 核心线程数
                8, // 最大线程数
                60L, // 空闲线程存活时间
                TimeUnit.SECONDS,
                new LinkedBlockingQueue<>(10), // 阻塞队列容量
                new ThreadPoolExecutor.CallerRunsPolicy() // 拒绝策略
        );

        long startTime = System.currentTimeMillis(); // 程序开始时间

        List<CompletableFuture<Void>> futures = new ArrayList<>();



        // 添加任务到列表中，使用自定义线程池执行
        futures.add(CompletableFuture.runAsync(() -> checkSystemMain(1), executorService));
        futures.add(CompletableFuture.runAsync(() -> checkBusinessRankLogApp(1), executorService));
        futures.add(CompletableFuture.runAsync(() -> checkBusinessRankLogVendor(1), executorService));
        futures.add(CompletableFuture.runAsync(() -> checkBusinessRankLogIpPortManage(1), executorService));
        futures.add(CompletableFuture.runAsync(() -> checkBusinessRankLogDomain(1), executorService));
        futures.add(CompletableFuture.runAsync(() -> checkBusinessRankLogWeb(1), executorService));
        futures.add(CompletableFuture.runAsync(() -> checkBusinessRankLogProduct(1), executorService));
        futures.add(CompletableFuture.runAsync(() -> checkBusinessRankLogBasis(1), executorService));
        futures.add(CompletableFuture.runAsync(() -> checkBusinessRankLogSystemBasis(1), executorService));

        try {
            // 等待所有任务完成
            CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])).join();

            // 检查每个任务是否出现异常
            for (CompletableFuture<Void> future : futures) {
                future.join(); // 抛出异常任务的异常
            }

            System.out.println("所有任务成功完成");
        } catch (Exception e) {
            Throwable cause = e.getCause(); // 获取原始异常
            String originalMessage = (cause != null) ? cause.getMessage() : e.getMessage();
            System.out.println("e = " + originalMessage);

            // 抛出新的异常，保留原始消息
            throw new BusinessException(originalMessage);

//            e.printStackTrace();
        } finally {
            executorService.shutdown(); // 优雅关闭线程池
        }

        long endTime = System.currentTimeMillis(); // 程序结束时间
        System.out.println("总执行时间: " + (endTime - startTime) + " 毫秒");
    }


    // 示例校验方法
    private static void checkSystemMain(int id) {
//        simulateCheck("checkSystemMain", id, false);
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }

    private static void checkBusinessRankLogApp(int id) {
//        simulateCheck("checkBusinessRankLogApp", id, true); // 模拟抛出异常
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }

//       throw new BusinessException("Validation failed123123");
    }



    private static void checkBusinessRankLogVendor(int id) {
//        simulateCheck("checkBusinessRankLogVendor", id, false);
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }


    private static void checkBusinessRankLogIpPortManage(int id) {
//        simulateCheck("checkBusinessRankLogIpPortManage", id, false);

        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }

    private static void checkBusinessRankLogDomain(int id) {
//        simulateCheck("checkBusinessRankLogDomain", id, false);

        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }

    private static void checkBusinessRankLogWeb(int id) {
//        simulateCheck("checkBusinessRankLogWeb", id, false);
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }

    private static void checkBusinessRankLogProduct(int id) {
//        simulateCheck("checkBusinessRankLogProduct", id, false);
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }

    private static void checkBusinessRankLogBasis(int id) {
//        simulateCheck("checkBusinessRankLogBasis", id, false);

        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }

    private static void checkBusinessRankLogSystemBasis(int id) {
//        simulateCheck("checkBusinessRankLogSystemBasis", id, false);

        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }

    // 模拟校验方法逻辑
    private static void simulateCheck(String taskName, int id, boolean shouldThrow) {
        try {
            System.out.println(taskName + " 开始执行");
            Thread.sleep(3000); // 模拟耗时操作
            if (shouldThrow) {
                throw new RuntimeException(taskName + " 抛出异常");
            }
            System.out.println(taskName + " 执行完成");
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }
    }










}
